// 防抖
export const debounce = (fn:Function, delay = 200,immediate = false) => {
    let timer:any = null;

    return function() {
        const args = arguments;
        if(immediate) {
            fn.apply(this, args);
            immediate = false;
        }
        if(timer) {
            clearTimeout(timer);
            timer = null;
        }
        timer = setTimeout(() => {
            fn.apply(this, args);
        }, delay);
    }
}

// 节流
export const throttle = (fn:Function, wait = 200) => {
    let previous = 0;
    return function() {
        const context:any = this;
        const args = arguments;
        const now = Date.now();
        if(now -previous > wait) {
            fn.apply(context, args);
            previous = now;
        }
    }
}